package tracker.ui

import org.eclipse.swt._
import org.eclipse.swt.graphics._
import org.eclipse.swt.widgets._
import org.eclipse.swt.layout._
import org.eclipse.swt.events._

object Main extends SystemController with bamboo.swt.SWTApplication {
  
  val tray = new TrayItem(display.getSystemTray(), SWT.NONE)
  val imageIdle = loadImage("resources/idle.png")
  val imageWorking = loadImage("resources/working.png")
  val menu = createMenu()
  val interpreter = new bamboo.swt.InterpreterView(shell)
  
  override def initialize = {
    super[SystemController].initialize()
    initializeTray()
    initializeShell()
    every(5000) {
      flushCurrentActivity() // updates activity's spent time
      updateTooltip()
    }
    asyncUI {
      loadStartupFile()
    }
 
  }
  
  def initializeShell() {
    shell.setText("Tracker")
    shell.setLayout(new FillLayout())
    shell.setSize(400, 400)
    shell.open()    
    hideOnMinimize()
  }
  
  private def hideOnMinimize() {
    shell.addShellListener(new ShellAdapter {
      override def shellIconified(e: ShellEvent) {
        shell.setVisible(false)
      }
    })
  }
  
  def loadStartupFile() {
    val file = new java.io.File(dataDir, "startup.scala")
    if (file.exists()) interpreter.load(file.getAbsolutePath)
    
    interpreter.import_("tracker.ui.Main._")
  }
  
  def updateTooltip() {
    val text = if (current != null) current.toString else "Time Tracker"
    tray.setToolTipText(text)
  }
  
  override def onStartProgress() {
    tray.setImage(imageWorking)
  }
  
  override def onStopProgress() {
    tray.setImage(imageIdle)
  }
  
  def initializeTray() = {
    tray.setImage(imageIdle)
    updateTooltip()
    onMenuDetect(tray) {
      menu.setVisible(true)
    }
    onSelection(tray) {
      shell.setMinimized(false)
      shell.setVisible(true)
    }
  }
  
  def createMenu() = {
    
    val menu = new Menu(shell, SWT.POP_UP)
    onShow(menu) {
      menu.getItems.foreach(_.dispose())
      val projects = subMenu(menu, "Projects")
      onShow(projects) {
        projects.getItems.foreach(_.dispose())
        system.projects.foreach(p => {
          val tasks = subMenu(projects, p.name)
          onShow(tasks) {
            tasks.getItems.foreach(_.dispose())
            system.tasks.forProject(p).foreach(t => {
              menuItem(tasks, t.name) {
                startProgress(t)
              }
            })
            separator(tasks)
            menuItem(tasks, "New Task ...") {
              newTask(p)
            }
          }
        })
      
        separator(projects)
      
        menuItem(projects, "New Project ...") {
          newProject()
        }
      }
      
      if (current != null) {
        separator(menu)
        menuItem(menu, "Stop working on '" + current + "'") {
          stopProgress()
        }
      }
      
      separator(menu)
      menuItem(menu, "Quit") {
        shell.close()
      }
    }
    
    menu
  }
  
  override def promptText(title: String) = prompt(title, 20, 5)
  
  override def dispose = {
    imageIdle.dispose()
    imageWorking.dispose()
    super[SystemController].dispose()
    super[SWTApplication].dispose()
  }
}